//
//  MainProperties.m
//  AppleCilent
//
//  Created by Nail Sharipov on 01/04/14.
//  Copyright (c) 2014 Nail Sharipov. All rights reserved.
//

#import "MainProperties.h"

@implementation MainProperties


+ (MainProperties*)getInstance
{
    static MainProperties* instance;
    
    @synchronized(self)
    {
        if (!instance)
            instance = [[MainProperties alloc] init];
        return instance;
    }
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        self.ip = [defaults objectForKey:@"ip"];
        self.port = [defaults objectForKey:@"port"];
        
        if (self.ip == nil) {
            self.ip = @"nailsharipov.asuscomm.com";
            [[NSUserDefaults standardUserDefaults] setObject:self.ip forKey:@"ip"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        if (self.port == nil) {
            self.port = @"8080";
            [[NSUserDefaults standardUserDefaults] setObject:self.port forKey:@"port"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        
        NSLog(@"%@:%@", self.ip, self.port);
//        self.ip = @"192.168.1.175";
//        self.port = @"8080";
        self.appName = @"WebClient";
    }
    return self;
}



- (NSString*)getImageFolder
{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    
    NSString* imagePath = [NSString stringWithFormat:@"%@/image", basePath];
    
    NSFileManager* fileManager = [NSFileManager defaultManager];
    
    BOOL isDir;
    BOOL isFileExists = [fileManager fileExistsAtPath:imagePath isDirectory:&isDir];
    if (!(isFileExists && isDir))
    {
        NSError* error;
        if (![fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:NO attributes:nil error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }
    }

    return imagePath;
}

- (NSString*)getRootUrl
{
    return [NSString stringWithFormat: @"http://%@:%@/%@/", self.ip, self.port, self.appName];
}

@end